package ncku.account.gui;

import java.awt.*;

import com.toedter.calendar.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.swing.*;

import ncku.account.data.Category;
import ncku.account.data.GlobalData;
import ncku.account.data.SubTransaction;
import ncku.account.data.SubTransactionList;
import ncku.account.event.Launcher;

//new Transaction的介面
public class newTransaction extends transactionModule {
	
    public newTransaction() {

        //按下Submit後的Listener
        JBSubmit.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) 
			{ 
				int selectIndex = categorySelection.categorySelItem.getSelectedIndex();  //取得categorySel的index
				if( (JDate.getDate() == null) || (InputDescriptionBox.getText().isEmpty()) || (InputAmount.getText() == "") || (selectIndex == -1)){  //沒有填寫完整資料
					JOptionPane.showMessageDialog(Launcher.frame, "資料尚未填寫完整！", "提示訊息", JOptionPane.ERROR_MESSAGE);
				}
				else{
					inputDate = JDate.getDate();  //讀取使用者輸入的日期
					inputDescription = InputDescriptionBox.getText();  //讀取使用者輸入的description
					try{
						inputAmount = Integer.parseInt(InputAmount.getText());  //讀取使用者輸入的amount
					}
					catch(Exception ex){
						JOptionPane.showMessageDialog(Launcher.frame, "金額須為大於零的整數！", "提示訊息", JOptionPane.ERROR_MESSAGE);
						return;
					}
					if(inputAmount<=0){
						JOptionPane.showMessageDialog(Launcher.frame, "金額須為大於零的整數！", "提示訊息", JOptionPane.ERROR_MESSAGE);
						return;
					}
					
					inputCategory = GlobalData.getCategory(selectIndex);
					int subTransSum = getSubTransSum();
					int amountTypeSel = amountType.getSelectedIndex();
					ArrayList<SubTransaction> inputSubTrans = subTransList.getArrayListSubTrans();  //Sub transaction definition
					
					if(inputSubTrans.size()!=0){
						if(subTransSum == inputAmount){
							inputAmount = checkAmount(amountTypeSel,inputAmount);
							inputSubTrans = checkSubTrans(amountTypeSel,inputSubTrans);
							
							GlobalData.add(inputDate, inputDescription, inputAmount, inputCategory, inputSubTrans);
							
							//cleaning variables
							subTransList = new SubTransactionList(); 				
							
							//切換panel ，先把原本的移除掉，然後加入新的
							Launcher.frame.getContentPane().remove(1);
							Launcher.frame.getContentPane().add (BorderLayout.CENTER,new showTransaction());
							Launcher.frame.getContentPane().doLayout();
						}
						else{
							JOptionPane.showMessageDialog(Launcher.frame, "Transaction金額與Subtransaction總金額不符", "提示訊息", JOptionPane.ERROR_MESSAGE);
						}	
					}
					else{
						inputAmount = checkAmount(amountTypeSel,inputAmount);
						GlobalData.add(inputDate, inputDescription, inputAmount, inputCategory, inputSubTrans);
						
						//cleaning variables
						subTransList = new SubTransactionList(); 				
						
						//切換panel ，先把原本的移除掉，然後加入新的
						Launcher.frame.getContentPane().remove(1);
						Launcher.frame.getContentPane().add (BorderLayout.CENTER,new showTransaction());
						Launcher.frame.getContentPane().doLayout();
					}
				}
			}}); 
        
        
        //按下Cancel後的Listener(清空內容)
        JBCancel.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) 
			{ 
				//cleaning variables
				subTransList = new SubTransactionList(); 
				Launcher.frame.getContentPane().remove(1);
				Launcher.frame.getContentPane().add (BorderLayout.CENTER,new mainPage());
				Launcher.frame.getContentPane().doLayout();
			}}); 
    }
}
